package com.hw.job_4;

import java.io.*;
import java.net.Socket;
import java.nio.Buffer;
import java.util.Scanner;

public class ClientTest {

    public static void main(String[] args) {

        Socket s = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        Scanner sc = null;

        try {
            s = new Socket("127.0.0.1", 8888);
            System.out.println("连接服务器成功");
            oos = new ObjectOutputStream(s.getOutputStream());
            ois = new ObjectInputStream(s.getInputStream());

            while (true) {
                System.out.println("请输入用户名和密码");
                sc = new Scanner(System.in);
                String userName = sc.next();
                String pwd = sc.next();

                UserMessage um = new UserMessage("check", new User(userName, pwd));
                oos.writeObject(um);
                System.out.println("客户端发送的信息是：" + um);

                // 获取服务端发来的内容
                Object obj = ois.readObject();
                if (obj instanceof UserMessage) {
                    System.out.println("客户端收到的信息是：" + (UserMessage)obj);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
